package com.chinaunicom.member.domain.service;

import com.alibaba.fastjson.JSON;
import com.chinaunicom.common.constants.Constants;
import com.chinaunicom.member.domain.model.entity.UserEntity;
import com.chinaunicom.member.domain.model.entity.UserLoginEntity;
import com.chinaunicom.member.domain.repository.IMemberRepository;
import com.chinaunicom.common.util.MD5;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author guomenghuai
 * @date 2024/1/8
 * @Description
 */
@Service
public class MemberService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private IMemberRepository memberRepository;

    public String login(UserLoginEntity userLogin) {
        userLogin.checkInit();
        UserEntity userEntity = memberRepository.queryUserByUserName(userLogin.getUsername());
        userEntity.checkPassword(userLogin.getPassword());

        String token = UUID.randomUUID().toString().replaceAll("-", "");
        stringRedisTemplate.opsForValue().set(Constants.UserCacheConstants.YJWL_TOKEN_KEY, token, 7, TimeUnit.DAYS);
        stringRedisTemplate.opsForValue().set(Constants.UserCacheConstants.YJWL_MEMBER_INFO_KEY + token, JSON.toJSONString(userEntity), 7, TimeUnit.DAYS);
        return token;
    }

}
